package com.cnofd.demo;

import java.io.File;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cnofd.agent.OfdSdkInit;
import com.cnofd.agent.OfdConvertAgent;
import com.cnofd.demo.util.ConfigUtil;
import com.cnofd.ofd.model.ResultInfo;

/**
 * @ClassName: OfdConvertAgentTest
 * @Description: OFD转Pdf、图片功能测试
 * @author cnofd
 * @date 2022年1月22日
 */
public class OfdConvertAgentTest {
    private static final Logger logger = LoggerFactory.getLogger(OfdConvertAgentTest.class);

    private static String cfgLicenseFile;
    private static String cfgTempDir;
    private static String cfgWorkDir;
    private static String cfgDataBasePath;

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        logger.info("01-启动：" + System.currentTimeMillis());

        // 读取配置文件
        Properties propConfig = ConfigUtil.getConfigProperties("init");
        cfgLicenseFile = ConfigUtil.getConfigKey(propConfig, "LicenseFile");
        cfgTempDir = ConfigUtil.getConfigKey(propConfig, "TempDir");
        cfgWorkDir = ConfigUtil.getConfigKey(propConfig, "WorkDir");
        cfgDataBasePath = ConfigUtil.getConfigKey(propConfig, "DataBasePath");

        // 加载授权文件
        if (!OfdSdkInit.cnofdSdkInit(System.getProperty("user.dir") + File.separator + cfgLicenseFile)) {
            // 授权失败退出
            return;
        }

        // 工作目录
        OfdSdkInit.setTempDir(cfgTempDir);
        OfdSdkInit.setWorkDir(cfgWorkDir);

        logger.info("02-完成初始化：" + (System.currentTimeMillis() - start));

        String sDataPath = cfgDataBasePath;
        String sOutPath = cfgDataBasePath;

        try {
            ResultInfo result = new ResultInfo();

            // 字体初始化
            OfdSdkInit.fontInit();
            // 字体初始化时，加载指定目录下的字体
            // OfdSdkInit.fontInit("C:/Windows/Fonts");

            // 为不规范的字体名创建映射
            OfdSdkInit.fontAddAlias("微软雅黑", "Microsoft YaHei UI");

            // OFD转PDF
            logger.info("03-开始OFD转PDF：" + (System.currentTimeMillis() - start));
            result = OfdConvertAgent.ofdToPdf(sDataPath + "cnofd-result-1.ofd", sOutPath + "Ofd2Pdf/cnofd-result.pdf");
            logger.info("03-结束OFD转PDF：" + (System.currentTimeMillis() - start) + result.getMessage());

            // OFD转图片
            logger.info("04-开始OFD转图片：" + (System.currentTimeMillis() - start));
            result = OfdConvertAgent.ofdToImage(sDataPath + "cnofd-result-1.ofd", sOutPath + "Ofd2Image", "JPG", 150);
            logger.info("04-结束OFD转图片：" + (System.currentTimeMillis() - start) + result.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //
        }
    }

}